﻿using System.Collections.Generic;
using TrackIT.Common.Providers;
using TrackIT.Common.Repositories;
using TrackIT.UI.Commands;

namespace TrackIT.Common.ViewModel
{
    public class ReportViewModel : ViewModelBase
    {
        private readonly IWorkLogRepository _workLogRepository;
        private readonly IFileAccessRepository _fileAccessRepository;
        private readonly IEmailProvider _emailProvider;

        public ReportType SelectedReportType { get; set; }

        public ReportViewModel(IWorkLogRepository workLogRepository, IFileAccessRepository fileAccessRepository, IEmailProvider emailProvider)
        {
            _workLogRepository = workLogRepository;
            _fileAccessRepository = fileAccessRepository;
            _emailProvider = emailProvider;

            _workLogRepository.Load();
            SelectedReportType = ReportType.Weekly;

            _createReportCommand = new Command(CreateReport);
        }

        #region Commands

        private readonly Command _createReportCommand;

        public Command CreateReportCommand
        {
            get { return _createReportCommand; }
        }

        private void CreateReport()
        {
            string filename = _workLogRepository.GetTemporaryFolderName(SelectedReportType);
            FilterDates filterDates = _workLogRepository.ResolveDatesFromReportType(SelectedReportType);
            List<string> summary = _workLogRepository.GetWorkSummary(filterDates);
            _fileAccessRepository.Save(summary, filename);
            string subject = _workLogRepository.GetReportName(SelectedReportType);
            _emailProvider.CreateEmail("blam", subject, "Attached is my work load summary", filename);
        }

        #endregion Commands
    }
}